package com.teamb6.shop.controller.admin;

import com.teamb6.shop.domain.Category;
import com.teamb6.shop.domain.CategoryExample;
import com.teamb6.shop.service.CateService;
import com.teamb6.shop.util.Msg;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CateService cateService;

    //分页查询商品分类
    @RequestMapping("/searchCategory")
    public String categoryPage(){return "category-list";}

    @GetMapping("/api/searchCategory")
    @ResponseBody
    public Msg searchCategoryResponse(@RequestParam("pageNum")Integer pageNum,
                                      @RequestParam("pageSize")Integer pageSize, @RequestParam("keyword")String keyword){
        PageHelper.startPage(pageNum,pageSize);
        List<Category> categories = cateService.listCategory(keyword);
        Msg msg = new Msg();
        return msg.addCategory(((Page)categories).getTotal(),categories);
    }

    //添加商品分类
    @RequestMapping("/addCategory")
    public String addCategory(Category category, Model model, RedirectAttributes redirectAttributes){
        List<Category> categories;
        CategoryExample example =new CategoryExample();
        example.or().andCatenameEqualTo(category.getCatename());
        categories =cateService.selectByExample(example);
        if(!categories.isEmpty()){
            redirectAttributes.addAttribute("Msg","分类已存在");
            return "redirect:/category/receive";
        }else{
            cateService.addCategory(category);
            redirectAttributes.addAttribute("Msg","分类添加成功");
            return "redirect:/category/receive";
        }
    }

    @RequestMapping("/receive")
    public String receiveMsg(@ModelAttribute("Msg")String msg, Model model){
        if(!msg.equals("")){
            model.addAttribute("msg",msg);
        }
        return "category-list";
    }

    //删除单行选中的商品分类
    @DeleteMapping("/delete")
    @ResponseBody
    public Msg deleteCategory(@RequestParam("id")Integer id){
        cateService.deleteCategoryByPrimaryKey(id);
        return Msg.success("删除商品分类成功");
    }

    //删除选中的多行商品分类，批量删除
    @RequestMapping("/multiDel")
    @ResponseBody
    public Msg deleteCategories(@RequestParam("ids")String ids){
        cateService.multiDelCategories(ids);
        return Msg.success("删除商品分类成功");
    }

    //得到选中的商品分类信息
    @GetMapping("/get")
    @ResponseBody
    public Category getCategory(@RequestParam("id")Integer id){
        return cateService.findCategoryByPrimaryKey(id);
    }

    //保存编辑后的商品分类信息
    @PostMapping("/edit")
    @ResponseBody
    public Msg updateCategory(Category category){
        cateService.updateCategory(category);
        return Msg.success("分类信息已保存");
    }
}
